package com.yylending.remotevideo.config;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bairuitech.anychat.AnyChatCoreSDK;
import com.bairuitech.anychat.AnyChatDefine;
import com.bairuitech.anychat.AnyChatObjectDefine;

public class DialogFactory {
	public static final int DIALOGID_CALLING = 1;
	public static final int DIALOGID_REQUEST = 2;
	public static final int DIALOGID_RESUME = 3;
	public static final int DIALOGID_CALLRESUME = 4;
	public static final int DIALOGID_ENDCALL = 5;
	public static final int DIALOGID_EXIT = 6;
	public static final int DIALOGID_CONFIG = 7;
	public static final int DIALOGID_MEETING_INVITE = 8;
	public static final int DIALOG_SERCLOSE = 1;
	public static final int DIALOG_AGAINLOGIN = 2;
	public static final int DIALOG_NETCLOSE = 3;

	public static int mCurrentDialogId = 0;
	private ConfigEntity configEntity;

	private Activity mContext;

	private static Dialog mDialog;
	private LayoutInflater mLayoutInlater;

	private TextView mTextViewTitle;
	private EditText mEditIP;
	private EditText mEditPort;

	public static DialogFactory mDialogFactory = new DialogFactory();

	private DialogFactory() {
	}

	public static void releaseDialog() {
		mCurrentDialogId = 0;
		mDialog = null;
		mDialogFactory = null;
	}

	public static DialogFactory getDialogFactory() {
		if (mDialogFactory == null) {
			mDialogFactory = new DialogFactory();
		}
		return mDialogFactory;
	}

	public static int getCurrentDialogId() {
		return mCurrentDialogId;
	}

	/**
	 * 获取指定类型的对话框实例
	 *
	 * @param dwDialogId 对话框类型
	 * @param object     对话框数据参数
	 * @param context    对话框位于的上下文
	 * @return 对话框实例
	 */
	public static Dialog getDialog(int dwDialogId, Object object, Activity context, CustomApplication mApplication) {
		mDialogFactory.initDialog(dwDialogId, object, context, mApplication);
		return mDialog;
	}

	public void initDialog(int dwDialogId, Object object, Activity context, CustomApplication mApplication) {
//		if (mContext != context) {
//			mContext = context;
//			mLayoutInlater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		}
//		mCurrentDialogId = dwDialogId;
//		mDialog = new Dialog(mContext, R.style.AnyChatDialog);
//		mDialog.setCanceledOnTouchOutside(false);
//		mDialog.setCancelable(false);
//		switch (dwDialogId) {
//			case DIALOGID_CALLING:
//				initCallingDialog(mDialog, object, mApplication);
//				break;
//			case DIALOGID_CALLRESUME:
//				initCallResume(mDialog, object);
//				break;
//			case DIALOGID_ENDCALL:
//				initEndSessionResumeDialg1(mDialog, context, object, mApplication);
//				break;
//			case DIALOGID_EXIT:
//				initQuitResumeDialg(mDialog, mApplication);
//				break;
//			case DIALOGID_REQUEST:
//				initCallReceivedDialg(mDialog, object, mApplication);
//				mDialog.setCancelable(false);
//				break;
//		}
	}

	/***
	 * 初始化呼叫对话框
	 */
	public void initCallingDialog(final Dialog dialog, Object object, final CustomApplication mApplication) {
//		final int userId = (Integer) object;
//		View view = mLayoutInlater.inflate(R.layout.dialog_calling, null);
//		ImageView buttonCancel = (ImageView) view.findViewById(R.id.btn_cancel);
//		buttonCancel.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				BussinessCenter.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, userId, AnyChatDefine.BRAC_ERRORCODE_SESSION_QUIT, 0, 0, "");
//				dialog.dismiss();
//				AnyChatCoreSDK.ObjectControl(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AGENT, mApplication.getUserID(), AnyChatObjectDefine.ANYCHAT_AGENT_CTRL_FINISHSERVICE, 0, 0, 0, 0, "");
//			}
//		});
//		String strTitle = mContext.getString(R.string.str_calling);
//		initDialogTitle(view, strTitle, userId);
//		dialog.setContentView(view);
	}

	/***
	 * 初始化呼叫确认对话框
	 */
	public void initCallResume(final Dialog dialog, Object object) {
//		final int userId = (Integer) object;
//		View view = mLayoutInlater.inflate(R.layout.dialog_call_resume, null);
//		Button btnCall = (Button) view.findViewById(R.id.btn_call);
//		btnCall.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				BussinessCenter.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REQUEST, userId, 0, 0, 0, "");
//				mDialog.dismiss();
//			}
//		});
//		String strTitle = "";
//		initDialogTitle(view, strTitle);
//		dialog.setContentView(view);
	}

	/***
	 * 初始化接收到呼叫请求对话框
	 */
	public void initCallReceivedDialg(final Dialog dialog, final Object object, CustomApplication mApplication) {
//		final int userId = (Integer) object;
//
//		View view = mLayoutInlater.inflate(R.layout.dialog_requesting, null);
//		TextView buttonAccept = (TextView) view.findViewById(R.id.btn_accept);
//		TextView buttonRefuse = (TextView) view.findViewById(R.id.btn_refuse);
//		TextView showView = (TextView) view.findViewById(R.id.txt_dialog_prompt);
//		showView.setText("座席" + mApplication.getTargetUserName() + "请求与您视频通话");
//		showView.setTextColor(Color.BLUE);
//		buttonAccept.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				BussinessCenter.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, userId, AnyChatDefine.BRAC_ERRORCODE_SUCCESS, 0, 0, "");
//				dialog.dismiss();
//			}
//		});
//
//		buttonRefuse.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				BussinessCenter.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, userId, AnyChatDefine.BRAC_ERRORCODE_SESSION_REFUSE, 0, 0, "");
//				dialog.dismiss();
//				BussinessCenter.getBussinessCenter().stopSessionMusic();
//				mContext.finish();
//			}
//		});
//
//		dialog.setContentView(view);
	}

	/***
	 * 初始化出挂断对话框
	 */
	public void initQuitResumeDialg(final Dialog dialog, final CustomApplication mApplication) {
//		View view = mLayoutInlater.inflate(R.layout.dialog_resumeorcancel, null);
//		Button buttonQuit = (Button) view.findViewById(R.id.btn_resume);
//		Button buttonCancel = (Button) view.findViewById(R.id.btn_cancel);
//		buttonQuit.setText(mContext.getString(R.string.str_exit));
//		buttonCancel.setText(mContext.getString(R.string.str_cancel));
//
//		buttonQuit.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				//暂停服务
//				Intent intent = new Intent();
//				intent.setAction(BaseConst.ACTION_BACK_SERVICE);
//				mContext.stopService(intent);
//				//退出队列
//				AnyChatCoreSDK.ObjectControl(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, mApplication.getCurrentQueueId(), AnyChatObjectDefine.ANYCHAT_QUEUE_CTRL_USERLEAVE, 0, 0, 0, 0, "");
//				dialog.dismiss();
//			}
//		});
//		buttonCancel.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				dialog.dismiss();
//			}
//		});
//		String strTitle = mContext.getString(R.string.str_exitresume);
//		initDialogTitle(view, strTitle);
//		dialog.setContentView(view);
	}

	/***
	 * 初始化通话结束确认对话框
	 */
	public void initEndSessionResumeDialg1(final Dialog dialog, final Activity context, final Object object, final CustomApplication mApplication) {
//		final int userId = (Integer) object;
//		Log.e("userID", userId + "");
//		View view = mLayoutInlater.inflate(R.layout.dialog_resumeorcancel, null);
//		TextView buttonPuase = (TextView) view.findViewById(R.id.btn_resume);
//		TextView buttonCancel = (TextView) view.findViewById(R.id.btn_cancel);
//		buttonPuase.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				//退出队列
//				AnyChatCoreSDK.ObjectControl(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, mApplication.getCurrentQueueId(), AnyChatObjectDefine.ANYCHAT_QUEUE_CTRL_USERLEAVE, 0, 0, 0, 0, "");
//				//调用内核方法执行；
//				BussinessCenter.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_FINISH, userId, 0, 0, BussinessCenter.selfUserId, "");
//				BaseMethod.showToast("正在结束视频通话...", context);
//			}
//		});
//		buttonCancel.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				dialog.dismiss();
//			}
//		});
//		String strTitle = mContext.getString(R.string.str_endsession);
//		initDialogTitle(view, strTitle);
//		dialog.setContentView(view);
	}

	private void initDialogTitle(View view, final String strTitle) {
//		mTextViewTitle = (TextView) view.findViewById(R.id.txt_dialog_prompt);
//		mTextViewTitle.setTextColor(Color.BLUE);
//		mTextViewTitle.setText(strTitle);
	}

	private void initDialogTitle(View view, final String strTitle, final int userId) {
//		mTextViewTitle = (TextView) view.findViewById(R.id.txt_dialog_prompt);
//		mTextViewTitle.setTextColor(Color.BLUE);
//		mTextViewTitle.setTextSize(20);
//		mTextViewTitle.setText(strTitle);
	}
}
